//
//  ManagerCheckController.m
//  Marketing
//
//  Created by Hanen 3G 01 on 16/3/2.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//管理员日志查看

#import "ManagerCheckController.h"
#import "DailRecordCell.h"
#import "RecordModel.h"
#import "AppDelegate.h"
#import "MyTabBarController.h"
#import "RecordDetailController.h"
#import "SelectViewController.h"
#import "UserModel.h"
#import "SelectPersonViewController.h"
#import "MJRefresh.h"


@interface ManagerCheckController ()<UITableViewDataSource,UITableViewDelegate,SelectStaffViewDelegate>
{
    UITableView  *_tableView;
    NSMutableArray  *_dataArray;
    
    int   pageCount;
     int   someOnePageCount;
    UserModel *usermodel;
//    SelectStaffView  *_selectStaffView;
//    UIView   *_backView;
    BOOL  isCheckOneRecod;

}
@end


@implementation ManagerCheckController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _dataArray = [NSMutableArray array];
    pageCount = 1;
    isCheckOneRecod = NO;
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithAction:@selector(popViewController)];
    self.navigationItem.rightBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_screen" WithAction:@selector(choosePerson:)];
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"日志查看"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addTableView];
    
    
}

- (void)initAllDataWithPage:(int)page WithRemove:(BOOL)IsRemove
{
    if (IsRemove) {
        [_dataArray removeAllObjects];
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"token" : TOKEN,@"uid" : @(UID),@"page" :@(page),@"rows" :@(10)}];
   
//    [DataTool sendGetWithUrl:MANAGER_ALL_RECORD_URL parameters:dict success:^(id data) {
//        id backData = CRMJsonParserWithData(data);
//        NSLog(@"%@",backData);
//        NSArray *dataArry = backData[@"list"];
//        for (int i = 0; i< dataArry.count ; i++) {
//            RecordModel * model = [[RecordModel alloc] init];
//            [model setValuesForKeysWithDictionary:dataArry[i]];
//            [_dataArray addObject:model];
//        }
//        [_tableView reloadData];
//        [_tableView.mj_header endRefreshing];
//        [_tableView.mj_header endRefreshing];
//        if (_dataArray.count == [backData[@"count"] intValue]) {
//            [_tableView.mj_footer endRefreshingWithNoMoreData];
//        }else{
//            [_tableView.mj_footer endRefreshing];
//        }
//      
//    } fail:^(NSError *error) {
//        NSLog(@"error %@ ",error.localizedDescription);
//        [_tableView.mj_header endRefreshing];
//        [_tableView.mj_footer endRefreshing];
//    }];
    
    [DataTool postWithUrl:MANAGER_ALL_RECORD_URL parameters:dict success:^(id data) {
        id backData = CRMJsonParserWithData(data);
        NSLog(@"%@",backData);
        if ([backData[@"code"] intValue] == 100) {
        NSArray *dataArry = backData[@"list"];
        for (int i = 0; i< dataArry.count ; i++) {
            RecordModel * model = [[RecordModel alloc] init];
            [model setValuesForKeysWithDictionary:dataArry[i]];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
//        [_tableView.mj_header endRefreshing];
        if (_dataArray.count == [backData[@"count"] intValue]) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [_tableView.mj_footer endRefreshing];
        }
        }else{
            [self.view makeToast:backData[@"message"]];
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
        }
    } fail:^(NSError *error) {
        NSLog(@"error %@ ",error.localizedDescription);
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}

- (void)addTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = graySectionColor;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //        [weakSelf loadNewData];
        if (isCheckOneRecod == NO) {//表示是查看all日志
            pageCount = 1;
            [self initAllDataWithPage:pageCount WithRemove:YES];
        }else{
            someOnePageCount = 1;
            [self checkOneRecordWith:[NSString stringWithFormat:@"%d",usermodel.uid] withRemove:YES];
        }
       
    }];
    // 马上进入刷新状态
        [_tableView.mj_header beginRefreshing];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //        [weakSelf loadMoreData];
        if (isCheckOneRecod ==NO) {
            [self loadMoreData];
        }else{
            someOnePageCount ++;
              [self checkOneRecordWith:[NSString stringWithFormat:@"%d",usermodel.uid] withRemove:NO];
        }
       
    }];
}
- (void)loadMoreData
{
    pageCount ++;
     [self initAllDataWithPage:pageCount WithRemove:NO];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DailRecordCell * cell = [DailRecordCell cellWithTableView:tableView];
    if(indexPath.row == _dataArray.count - 1){
        cell.islast = YES;
    }else{
        cell.islast = NO;
    }
    if (_dataArray.count != 0) {
       cell.model = _dataArray[indexPath.row];
    }
    cell.backgroundColor = [UIColor whiteColor];
    

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //dfdf234324
    return  [DailRecordCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    RecordDetailController * recordDetaVC = [[RecordDetailController alloc] init];
    if (_dataArray.count!= 0) {
        RecordModel *model = _dataArray[indexPath.row];
         recordDetaVC.recId = model.recordId;
    }
   
//        NSLog(@"%d,%d",_dataArray.count,model.recordId);
   
    
    [self.navigationController pushViewController:recordDetaVC animated:YES];
    
}
//点击右上角的筛选
- (void)choosePerson:(UIButton*)btn
{
  
//    SelectViewController *selectViewController = [[SelectViewController alloc]init];
//    self.modalPresentationStyle =  UIModalPresentationPageSheet;
//    
//    selectViewController.delegate = self;
//    
//    [self presentViewController:selectViewController animated:YES completion:^{
//        selectViewController.view.superview.frame = CGRectMake(0, 0, KSCreenW, KSCreenH);
//    }];
//    SelectViewController *selectViewController = [[SelectViewController alloc]init];
//    selectViewController.modalPresentationStyle =  UIModalPresentationPageSheet;
//    selectViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
//    selectViewController.delegate = self;
//    
//    [self.navigationController pushViewController:selectViewController animated:YES];
//    UIImage *image;
//    @autoreleasepool {
//        image = [ViewTool ScreenShotFromView:[[UIApplication sharedApplication] windows][0]];
//    }
    SelectPersonViewController * VC = [[SelectPersonViewController alloc] init];
    self.modalPresentationStyle =  UIModalPresentationPageSheet;
//    VC.screenShot = image;
    VC.delegate = self;
   
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)getSelectedStaff:(NSArray *)array
{
    if (array.count != 0) {
        usermodel = array[0];
        isCheckOneRecod = YES;
        [_tableView.mj_header beginRefreshing];
//        [self checkOneRecordWith:[NSString stringWithFormat:@"%d",usermodel.uid] withRemove:YES];
    }
}
- (void)checkOneRecordWith:(NSString *)uid withRemove:(BOOL)remove
{
    if (remove) {
        [_dataArray removeAllObjects];
    }
   
    NSDictionary * dict = @{@"token" : TOKEN,@"uid" : @(UID),@"yuid" : @([uid intValue]),@"page" :@(someOnePageCount) ,@"rows" :@(10)};
    NSLog(@"查看某一个员工日志列表参数%@",dict);
//    [DataTool sendGetWithUrl:MANAGER_CHECK_ONEWORKER_ALLREORD_URL parameters:dict success:^(id data) {
//        id backData = CRMJsonParserWithData(data);
//        NSLog(@"查看日志列表返回数据%@",backData);
//        NSArray *dataArry = backData[@"list"];
//        for (int i = 0; i< dataArry.count ; i++) {
//            RecordModel * model = [[RecordModel alloc] init];
//            [model setValuesForKeysWithDictionary:dataArry[i]];
//            [_dataArray addObject:model];
//        }
//        [_tableView reloadData];
//        [_tableView.mj_header endRefreshing];
//        [_tableView.mj_header endRefreshing];
//        if (_dataArray.count == [backData[@"count"] intValue]) {
//            [_tableView.mj_footer endRefreshingWithNoMoreData];
//        }else{
//            [_tableView.mj_footer endRefreshing];
//        }
////        [_tableView.mj_footer endRefreshing];
//    } fail:^(NSError *error) {
//        NSLog(@"error %@ ",error.localizedDescription);
//        [_tableView.mj_header endRefreshing];
//        [_tableView.mj_footer endRefreshing];
//    }];
    
    [DataTool postWithUrl:MANAGER_CHECK_ONEWORKER_ALLREORD_URL parameters:dict success:^(id data) {
        id backData = CRMJsonParserWithData(data);
        NSLog(@"查看日志列表返回数据%@",backData);
        NSArray *dataArry = backData[@"list"];
        for (int i = 0; i< dataArry.count ; i++) {
            RecordModel * model = [[RecordModel alloc] init];
            [model setValuesForKeysWithDictionary:dataArry[i]];
            [_dataArray addObject:model];
        }
        [_tableView reloadData];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_header endRefreshing];
        if (_dataArray.count == [backData[@"count"] intValue]) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [_tableView.mj_footer endRefreshing];
        }

    } fail:^(NSError *error) {
        NSLog(@"error %@ ",error.localizedDescription);
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
//    [_tableView.mj_header beginRefreshing];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)popViewController
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
